import http from './interface'
import global from '@/utils/global'
import { hideLoading } from '@/utils/helper';
import { useShopStore } from '@/stores/shopStore';
import { useUserStore } from '@/stores/commonStore';
const shopStore = useShopStore();
const userStore = useUserStore();
import {
	env
} from '../env.js';
var publicEventId = global.QR_NEW_MEAL_EVENT_ID; //事件定义
/**
 * 字符串是否含有html标签的检测中
 */
function checkHtml(htmlStr) {
	var reg = /<[^>]+>/g;
	return reg.test(htmlStr);
}
// 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
export const request = (config) => {
	//console.log("request请求url和参数：",JSON.stringify(config))
	let contentType = 'application/x-www-form-urlencoded;charset=UTF-8';

	if (config.header)
		contentType = config.header.ContentType
	//设置请求时长
	let sendDate = new Date().getTime();
	let groupTike = getTiket() || ''; //原

	// let groupTike = '483837CE6CFF94E5EB2E999B99782FD52E5DAFCFDA8BF52E26B83C67AB055BD90E0AAFC8192FC8E96135CDBEAC737ECA'
	// let ntiket = uni.getStorageSync(global.USER_TKT)||'';
	// !ntiket&&uni.setStorageSync(global.USER_TKT,groupTike)

	// let groupMemberId = getGlobalUser().groupMemberId || '';
	//请求参数:门店ID(订单列表)
	/* if (config.data)
		config.data.sellerMemberId = global.getSellerMemberId || ''; */

	//设置请求前拦截器
	http.interceptor.request = (content) => {
		content.header = {
			"Content-Type": contentType,
			"tiket": groupTike || '',
			// "Cookie":"8113bf4d616a995dbe086d10019ff5e1",
			"sellerMemberId": shopStore
			.curSellerMemberId, //shopStore.curSellerMemberId优先1扫码入驻,2缓存门店uni.getStorageSync(global.QR_REGISTER_SELL_MEMBERID) ? uni.getStorageSync(global.QR_REGISTER_SELL_MEMBERID) : (uni.getStorageSync(global.SHOP_CURRENT_ID).nmember_id || "1")
		}
	}
	//设置请求结束后拦截器
	http.interceptor.response = (response) => {
		let receiveDate = new Date().getTime();
		let responseTimeMs = receiveDate - sendDate;
		let hasLogin = false; //是否登录标记
		console.log('连接成功服务器响应时长:', response.config.url,responseTimeMs)
		try {
			hasLogin = uni.getStorageSync(global.IS_LOGIN) || false;
		} catch (e) {}

		const regHtml = JSON.stringify(response.data) || '';
		// console.log("regHtmlregHtml:",regHtml)
		const isStepServer = checkHtml(regHtml) ? true : false;
		if (isStepServer && !response.data.result && !(regHtml.indexOf('502') == -1)) {
			hideLoading()
			setTimeout(() => {
				wx.showToast({
					title: '系统正在升级中,请稍后再试哦',
					icon: "none",
				});
				setTimeout(() => {
					wx.hideToast();
				}, 5000)
			}, 0);
			return;
		} else {
			// 登录拦截
			if (response.data && (response.data.code && response.data.code == '-1')) {
				userStore.setUserInfo({})
				uni.removeStorageSync(global.IS_LOGIN);
				uni.setStorageSync(global.USER_TIKET,'');
				//var loginUrl = "pages/login/login/login";
				//防止重复进入登录UI
				//if (loginUrl != prevPage.route) {
				if (!global.CENTER_LOGIN_PAGE) {
					global.CENTER_LOGIN_PAGE = true; //进入登录页面	
					global.IS_FROM_LOGIN = true;
					console.log('进入登录页面+')
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				} else {
					console.log("打印全局页面是否进入：" + global.CENTER_LOGIN_PAGE);
				}
				//}
				/*} */
			}
			if (response.data && (response.data.msgCode && response.data.msgCode == '-2') && !global
				.ARCHIVES_TIPS) {
				global.ARCHIVES_TIPS = true
				uni.removeStorageSync(global.ARCHIVES_ID)
				uni.removeStorageSync(global.PATTERN_ID);
				uni.showModal({
					title: '提示',
					content: '未发现病友档案',
					// showCancel:false,
					cancelText: '再看看',
					confirmText: '去添加',
					success: res => {
						if (res.confirm) {
							// global.ARCHIVES_TIPS = false
							uni.navigateTo({
								url: '/pages/user/addArchives/addArchives?pageSource=1001'
							})
							// this.navigateTo('/user/addArchives/addArchives?pageSource=1001',true)
						}
					},
					complete: res => {
						setTimeout(() => {
							global.ARCHIVES_TIPS = false
						}, 1000)
					}
				});
				return
			}
			//console.log("全局global打印:【"+(config.baseUrl+config.url)+"】请求Url参数："+(JSON.stringify(config.data) || '')+" Url接口Result："+JSON.stringify(response.data) || '')
			return response;
		}
	}
	let formData = {
		baseUrl: config.baseUrl,
		url: config.url,
		dataType: 'json',
		data: config.data,
		method: config.method,
	}
	//console.log("Dev:"+process.env.NODE_ENV+"formData_data:" + JSON.stringify(config.data))
	//responseType
	if (config.responseType) {
		formData.responseType = config.responseType
	}

	//成功请求
	return http.request(formData)
}
export function bindEvent(data) {
	let eventqrId = uni.getStorageSync(global.QR_NEW_MEAL_EVENT_ID) || {};
	if (eventqrId.expiration) {
		if (new Date().getTime() - eventqrId.expiration >= 0) {
			uni.removeStorageSync(global.QR_NEW_MEAL_EVENT_ID);
		} else {
			data.event_id_1 = eventqrId.event_id_1 || '';
			data.event_id_2 = eventqrId.event_id_2 || '';
		}
	}
	return data
}
export function getGlobalUser() {
	var loginUser = '';
	var globalUser = uni.getStorageSync(global.USER_INFO) || '';
	// console.log("获取全局globalUser:" + globalUser);
	try {
		loginUser = JSON.parse(globalUser);
	} catch (e) {}
	return globalUser ? loginUser : '';
}
// 获取tiket-暂时
export function getTiket() {
	var loginUser = '';
	var globalUser = uni.getStorageSync(global.USER_TIKET) || '';
	return globalUser || '';
}
// 登录-暂
// export const login = (data) => {
// 	return request({
// 		baseUrl: env.localApi,
// 		url: '/m/login.do',
// 		method: 'GET',
// 		data,
// 		// handle:true
// 	})
// }
//搜索门店(门店列表)
// export const queryMemberList = (data) => {
// 	return request({
// 		baseUrl: env.subscribeApi,
// 		url: '/api/pub/seller/member/query.json',
// 		// url: '/api/member/queryMemberList.json',
// 		method: 'POST',
// 		data
// 	})
// }

//person1-start===>
//获取手机号-登录
// export const getWxPhone = (data) => {
// 	return request({
// 		baseUrl: env.loginApi,
// 		url: '/v2/authorize/phone.json',
// 		method: 'POST',
// 		data
// 	})
// }
//wxcode-登录
// export const getWxCodeLogin = (data) => {
// 	return request({
// 		baseUrl: env.loginApi,
// 		url: '/v2/authorize/login.json',
// 		method: 'POST',
// 		data
// 	})
// }
//注册-登录
// export const regMemberAct = (data) => {
// 	let ndata = data || {};
// 	let params = uni.getStorageSync(global.QR_PARAMS) || {};
// 	ndata.fromAppId = '11';
// 	ndata.channel = '1114';
// 	ndata.systemId = 'S039';
// 	if (params.expiration) {
// 		if (new Date().getTime() - params.expiration >= 0) {
// 			uni.removeStorageSync(global.QR_PARAMS);
// 		} else {
// 			ndata.event_id_1 = params.event_id_1 || ''; //事件1
// 			ndata.event_id_2 = params.event_id_2 || ''; //事件2
// 			ndata.share_id = params.share_id || '';
// 			ndata.shareMemberId = params.shareMemberId || ''; //推荐人
// 			ndata.systemId = params.system_id || 'S039';
// 			ndata.sellMemberId = params.sellMemberId || '';

// 		}
// 	}
// 	let formData = {
// 		baseUrl: env.loginApi,
// 		url: '/v2/authorize/reg.json',
// 		dataType: 'json',
// 		data: ndata,
// 	}
// 	return request(formData)

// }
// 门店列表
export const queryMemberLists = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/pub/seller/member/query.json',
		method: 'POST',
		data
	})
}

export const wx_fPhone = (data) => {
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/xcx/phone.json',
		data,
		method: 'POST'
	})
}
export const getMemberLoginMst = (data) => {
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/member/get.json',
		method: 'POST',
		data,
	})
}
export const registerMembert = (data) => {
	
	let params = uni.getStorageSync(global.QR_PARAMS) || {};
	if (params.expiration) {
		if (new Date().getTime() - params.expiration >= 0) {
			uni.removeStorageSync(global.QR_PARAMS);
		} else {
			data.vreg_one = params.event_id || '';
			data.sell_member_id = params.sell_member_id || '';
			data.userid = params.share_member_id||'';
		}
	}
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/xcx/reg.json',
		method: 'POST',
		data,
	})
}
export const getServerLogint = (data) => {
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/xcx/getLogin.json',
		method: 'POST',
		data,
	})
}
export const wxLoginOutt = (data) => {
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/xcx/logout.json',
		method: 'POST',
		data,
	})
}
export const member_tikett = (data) => {
	return request({
		baseUrl: env.loginApi,
		url: '/authorize/xcx/queryMemberByTiket.json',
		data,
	})
}
//技师编号是否展示
export const getCraftsmanSwitcht = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/pub/seller/member/strategy.json',///api/pub/craftsman/switch.json
		method: 'POST',
		data,
	})
}
//获取订单列表
export const getOrderListt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/buy/bill/list.json',
		method: 'POST',
		data,
	})
}

// 更改订单状态
export const changeOrderStatust = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/buy/bill/status.json',
		method: 'POST',
		data,
	})
}
//取消订单
export const cancelOdert = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/subscribe/bill/cancel.json',
		method: 'POST',
		data,
	})
}
//分享参数:查询分享明细
export const getShareDetailById = (data) => {
	return request({
		baseUrl: env.MemberApi,
		url: '/share/details.json',
		method: 'POST',
		data,
	})
}
//获取项目列表
export const getSubGoodsListt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/subscribe/goods/limit/query.json',
		method: 'POST',
		data
	})
}
//获得明细-根据编号no
export const getCraftsmanDetailt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/pub/craftsman/get.json',
		method: 'POST',
		data,
	})
}
//获取时间片列表
export const getTimeSilceListt = (data, type = '') => {
	let url = type === 'initdian' || type === 'tap_dian' ? '/api/pub/craftsman/goods/price/v2.json' :
		'/api/cw/where/list.json'
	return request({
		baseUrl: env.subscribeApi,
		// url: '/api/pub/unit/list.json',
		url: url,
		method: 'POST',
		data,
	})
}
//修改订单联系方式
export const changeContactt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/buy/bill/changePhone.json',
		method: 'POST',
		data,
	})
}
// 更改项目
export const changeCraftsmanSevt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/bill/service/goods/change.json',
		method: 'POST',
		data,
	})
}
//获取订单详情
export const getOrderDetailt = (data) => {
	return request({
		baseUrl: env.subscribeApi,
		url: '/api/buy/bill/get.json',
		method: 'POST',
		data,
	})
}
// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {
	queryMemberLists,
	wx_fPhone,
	getMemberLoginMst,
	registerMembert,
	getServerLogint,
	wxLoginOutt,
	member_tikett,
	getCraftsmanSwitcht,
	getOrderListt,
	changeOrderStatust,
	cancelOdert,
	getShareDetailById,
	getSubGoodsListt,
	getCraftsmanDetailt,
	getTimeSilceListt,
	changeContactt,
	changeCraftsmanSevt,
	getOrderDetailt
}